package com.qingfeng.controller.shop;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.qingfeng.biz.comment.common.dto.ResultPageDTO;
import com.qingfeng.business.biz.shop.IBussShopInfoService;
import com.qingfeng.common.dto.ResultDTO;
import com.qingfeng.constant.enums.ErrorMessages;
import com.qingfeng.controller.common.BaseController;
import com.qingfeng.controller.common.ViewPages;
import com.qingfeng.dao.dto.shop.BussShopInfoQueryDTO;
import com.qingfeng.dao.entity.shop.BussShopInfo;
import com.qingfeng.dao.framework.Page;
import com.qingfeng.exception.BusinessException;
import com.qingfeng.utils.DictUtil;
import com.qingfeng.utils.LogUtil;

@Controller
@RequestMapping("bussShopInfo")
public class BussShopInfoController extends BaseController{
    @Autowired
    IBussShopInfoService  bussShopInfoService;
    
    @RequestMapping(value = "bussShopInfoManage.htm")
    public String bussShopInfoManage(ModelMap modelMap,HttpSession session) {
        return ViewPages.SHOPINFO_VIEW;
    }
    
    /**
     * 门店查询
     * @param response
     * @param query
     * @param page
     */
    @RequestMapping("querybussShopInfoPage.json")
    public void querybussShopInfoPage(HttpServletResponse response,BussShopInfoQueryDTO query,Page page){ 
        
        try {
            query.setOrderType("default");
            ResultPageDTO<List<BussShopInfo>>  resultDTO = bussShopInfoService.selectBussShopInfoPage(query, page);
            if(resultDTO.isSuccess()){
                this.sendJsonSuccessPage(resultDTO.getData(), resultDTO.getTotalRows(),response);
            }else{
                sendJsonError(ErrorMessages.SELECT_FAILURE,response);
            }
        } catch (BusinessException e){
            LogUtil.error(e.getMessage(), e);
            this.sendJsonError(e.getError(), response);
        } catch (Exception e) {
            LogUtil.error("门店查询失败",e);
            this.sendJsonError(ErrorMessages.SELECT_FAILURE, response);
        }
    }
    /**
     * 门店保存
     * @param response
     * @param session
     * @param shopInfo
     */
    @RequestMapping("insertShopInfo.json")
    public void insertShopInfo(HttpServletResponse response, HttpSession session,BussShopInfo shopInfo){
        try {
           shopInfo.setIsDelete(DictUtil.yesOrNo.N.getValue());
           shopInfo.setCreateTime(new Date());
           shopInfo.setCreateUser(null);
           shopInfo.setLastModifyTime(new Date());
           shopInfo.setLastModifyUser(null);
           ResultDTO<String> dto= bussShopInfoService.insertBussShopInfo(shopInfo);
           if(dto.isSuccess()){
               sendJsonSuccess(response);
           }else{
               sendJsonError(ErrorMessages.INSERT_FAILURE, response);
           }
        } catch (BusinessException e){
            LogUtil.error(e.getMessage(), e);
            this.sendJsonError(e.getError(), response);
        } catch (Exception e) {
            LogUtil.error("门店保存失败",e);
            this.sendJsonError(ErrorMessages.INSERT_FAILURE, response);
        }
    }
    /**
     * 门店修改
     * @param response
     * @param session
     * @param shopInfo
     */
    @RequestMapping("updateShopInfo.json")
    public void updateShopInfo(HttpServletResponse response,HttpSession session,BussShopInfo shopInfo){
        try {
            shopInfo.setLastModifyTime(new Date());
            shopInfo.setLastModifyUser(null);
            ResultDTO<String>  resultDTO = bussShopInfoService.updateBussShopInfo(shopInfo);
            if(resultDTO.isSuccess()){
                sendJsonSuccess(resultDTO.getData(),response);
            }else{
                sendJsonError(ErrorMessages.UPDATE_FAILURE, response);
            }
        } catch (BusinessException e){
            LogUtil.error(e.getMessage(), e);
            this.sendJsonError(e.getError(), response);
        } catch (Exception e) {
            LogUtil.error("修改门店信息失败",e);
            sendJsonError(ErrorMessages.UPDATE_FAILURE, response);
        }
    }
    
   /**
    * 门店删除
    * @param response
    * @param id
    * @param session
    */
    @RequestMapping("deleteShopInfo.json")
    public void deleteShopInfo(HttpServletResponse response,Integer id,HttpSession session){

        try {
            ResultDTO<String>  resultDTO = bussShopInfoService.deleteBussShopInfo(id, null);
            if(resultDTO.isSuccess()){
                sendJsonSuccess(resultDTO.getData(),response);
            }else{
                sendJsonError(ErrorMessages.DELETE_FAILURE, response);
            }
        } catch (BusinessException e){
            LogUtil.error(e.getMessage(), e);
            this.sendJsonError(e.getError(), response);
        } catch (Exception e) {
            LogUtil.error("删除门店信息失败",e);
            sendJsonError(ErrorMessages.DELETE_FAILURE, response);
        }
    }
    
 
}
